package com.example.performance.r2dbc;

import com.example.performance.r2dbc.compare.R2DBCService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zpMeng
 * @since 6/19/2025
 */
@Slf4j
@RestController
@RequestMapping("r2dbc")
public class R2DBCController {

    @Resource
    private R2DBCService r2DBCService;

    @GetMapping("serial")
    public void serialBean() {
        log.info("r2dbc serial test started");
        warmUp();

        r2DBCService.testSerialPerformance();

        log.info("r2dbc serial test ended");

    }

    @GetMapping("parallel")
    public void parallel(int start, int end, int nodeSize) throws Exception {
        log.info("r2dbc parallel test started: {} -- {} -- {}", start, end, nodeSize);

        warmUp();

        r2DBCService.testParallelPerformance(start, end, nodeSize);

        log.info("r2dbc parallel test ended: {} -- {} -- {}", start, end, nodeSize);
    }

    private void warmUp() {
        log.info("warm up started");
        for (int i = 0; i < 10; i++) {
            r2DBCService.executeOneR2DBCQuery(0, 1000L);
        }
        log.info("warm up ended");
    }

}